﻿@using OBUCare.Web.Infrastructure;
@using OBUCare.Core.Models;
@using OBUCare.Core.BL;
@using OBUCare.Core.DataAccess;
@using OBUCare.Core.Common 
@model AgentInfo
@{
    ViewBag.Title = "Transfer";
    ObuDropDownListHlper ddlHelper = new ObuDropDownListHlper();    
}

<h2 style="text-align:center;height:60px;">元器件划拨登记</h2>

@using (Html.BeginForm("ConfirmTransfer", "Parts", FormMethod.Post, new { @id = "form_id" }))
{ 
    
    
    <table style="width:100%;">
            <tr>
                <td style="width:15%">
                    源营业厅：
                </td>
                <td style="width:35%">
                    @Html.Telerik().DropDownList().Name("sAgent").HtmlAttributes(new { @style = "Width:230px;" }).BindTo(ddlHelper.GetAgents(Model.AgentNo, false, null, null, new KeyValuePair<string, string>(Model.AgentNo, Model.AgentName))).ClientEvents(e=>e.OnChange("OnSAgentChange"))
                </td>
                 <td style="width:15%">
                    &nbsp;
                </td>
                <td style="width:35%">
                   &nbsp; 
                </td>
            </tr>
        <tr style="height:20px;">
            <td colspan="3">

            </td>

        </tr>
    </table>
    
  

     <div id="divContainer" style="width: 100%">
        @Html.Partial("TransferGridView",ViewBag.Items as IList<View_PartsStock>) 
     </div>
    <table style="width:100%">
            <tr style="height:20px;">
                <td colspan="3">

                </td>

            </tr>

            <tr>
                <td style="width:15%">
                    发行代理：
                </td>
                <td style="width:35%">
                    @Html.Telerik().DropDownList().Name("Agent").HtmlAttributes(new { @style = "Width:230px;" }).BindTo(ddlHelper.GetAgents(Model.AgentNo, false, null, null, new KeyValuePair<string, string>(Model.AgentNo, Model.AgentName)))
                </td>
                <td style="width:15%">
                    操作员：
                </td>
                <td style="width:35%">
                    <label id="lblOper" >@UIUtility.GetCurrentUser().OPERATOR_NAME</label>
                </td>
            </tr>

       

            <tr>
                <td>
                    划拨数量：
                </td>
                <td>
                   <label id="lblTransfer">0</label>
                </td>
                <td>
                    备注：
                </td>
                <td>
                    <input type="text" id="txtComments" name="txtComments"/>
                </td>
            </tr>
           <tr>
               <td style=" width:70%" colspan="3">
                   <input type ="hidden"  id="hidSql"/>
               </td>
               <td align="center">
                   <input type="button" value="划拨"  onclick="submitForm();"/>
               </td>
           </tr>
        </table>

}


@* <div>
        @(Html.Telerik().Window()
        .Name("Window")       
        .Buttons(buttons => buttons.Maximize().Close())
        .Width(900)
        .Draggable(true)
        .Title("划拨单")
        .Visible(false)
        .ClientEvents(events => events.OnClose("Window_onClose"))        
        .Content
            (
                @<text>
                    @Html.Partial("TransferPrint")
                </text>
            )
        )
</div>*@

<script type="text/javascript">
   
    function submitForm() {
        
        var form = $("#form_id");

        if ($("#sAgent").data("tDropDownList").value() == $("#Agent").data("tDropDownList").value())
        {

            alert("目的营业厅与源营业厅，不能是同一个营业厅，请选择不同的营业厅");
            return false;
        }

        var allCount = document.getElementById("lblTransfer").innerText;
        if (allCount == "0") {
            alert("请输入划拨数量");
            return false;
        }
        dataString = form.serialize();
        //alert(dataString);
        $.ajax({
            type: "POST",
            url: form.attr('action'),
            data: dataString,
            cache: false,
            dataType: "json",
            success: function (data) {

                if (data.success) {

                    
                    alert("划拨成功");
                    window.location.href = "/Parts/Transfer/";
                    //debugger;
                    OpenPrintWin(data.sql);

                    //$("#form_id")[0].reset();
                    //window.location.href = "/Parts/Transfer/";
                }
                else {
                    alert('划拨失败：' + data.ErrorMsg)
                }
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                var err = XMLHttpRequest + "<br />" + textStatus + "<br />" + errorThrown;
                alert(err);
            }
        });

    }



    function sumCount() {
        var list = $("#Grid").find("TR");
        var sumTransfer = 0;

        for (var i = 1; i < list.length; i++) {

            var count = list[i].children[5].children[0].children[1].value;
            if (count != "0" && $.trim(count) != "") {
                    sumTransfer += parseInt($.trim(count));
            }
             
        }
       document.getElementById("lblTransfer").innerText = sumTransfer;
    }

    function OnSAgentChange(e)
    {
        //var grid = $('#Grid').data('tGrid');        
        //grid.rebind();
        var sAgent = $("#sAgent").data("tDropDownList").value();
        var id = sAgent;
        var d = new Date();
        var dateTimeNow = d.getTime();
        var purl = '/parts/TransferGridView/' + id+","+dateTimeNow;

        $('#divContainer').load(purl, function (response, status) {
            debugger
            //alert(status);
            if (status == "success") {
                
            }
            else {
                alert("加载失败");
            }
        })

    }


    //function onError(e) {
    //    //debugger;
    //    e.preventDefault();
    //    /*if (e.XMLHttpRequest.status == "505")
    //    alert("系统字段不允许删除");
    //    else if (e.XMLHttpRequest.status == "506")
    //    alert("系统表不允许删除");
    //    else*/
    //    alert(e.XMLHttpRequest.getResponseHeader("message"));
    //}

   

    //function onDataBinding(e) {
    //    var grid = $('#Grid').data('tGrid')
    //    index = (grid.currentPage - 1) * grid.pageSize;
        
    //    var sAgent = $("#sAgent").data("tDropDownList").value();

    //    e.data = {
    //        // the key ("category") specifies the variable name of the action method which will contain the specified value
    //        id: sAgent
    //    };
    //}


    //打开打印页
    function OpenPrintWin(sql) {
        /*$("#hidSql").val(sql);

        var grid = $('#pGrid').data('tGrid')
        grid.rebind();
        
        $('#lblsAgent').text($("#sAgent").data("tDropDownList").text());
        $('#lbltAgent').text($("#Agent").data("tDropDownList").text());
        var window = $("#Window").data("tWindow");
        window.center();
        window.open();*/
        window.open("/parts/TransferPrint/" + sql);
    }

    //关闭窗口时，跳转到初始页面
    function Window_onClose() {
        window.location.href = "/Parts/Transfer/";
    }
</script>


@{
    ddlHelper.Dispose();    
}
